package QQ;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author ╱―★神★―╲
 *
 *		基于QQ kjava3版本 的简单手机QQ协议
 *
 */
public class QQKJava3 {
	
	public static final String IP = "58.60.12.177";
	public static final int PORT = 14000;

	
	//登录
	
	public String login(String qq,String pwd) {
		return "VER=1.4&CON=1&CMD=Login&SEQ="+seq()+"&UIN="+qq+"&PS="+Md5.exec(pwd.getBytes())+"&M5=1&LG=0&LC=812822641C978097&GD=5MWX2PF3FOVGTP6B&CKE=\n";
	}
	
	
	// 登录返回消息的解析
	//0=密码错误
	//1=登录成功
	//2=需要验证码
	public int loginReturn(String msg) {
		
		if( msg.indexOf("Password error") !=-1)  {
			return 0;
		}
		
		if( msg.indexOf("VERIFYCODE") !=-1  && msg.length() > 70) {
			
			String vc = msg.substring(msg.indexOf("&VC=")+4);
			
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			
			for(int i = 0 ; i < vc.length() ; i+=2) {
				int b = Integer.parseInt(vc.substring(i,i+2),16); // 16转10
				bos.write(  b & 0xFF ); // 取字节
			}
	
			try {
				bos.flush();
				bos.close();
				vc_png = bos.toByteArray();
			} catch (IOException e) {
				e.printStackTrace();
			}
	
			return 2;
		}
		
		return 1;
	}
	
	private byte[] vc_png;

	
	// PNG图片  字节数组
	public byte[] verifyCode() {
		return vc_png;
	}
	
	
	// 带验证码登录
	public String loginAndVC(String qq,String code) {
		return "VER=1.4&CON=1&CMD=VERIFYCODE&SEQ="+seq()+"&UIN="+qq +"&SID=&XP=C4CA4238A0B92382&SC=2&VC="+code+"\n";
	}
	
	
	
	//注销
	public String canceled(String qq) {
		return "VER=1.4&CON=1&CMD=Logout&SEQ="+seq()+"&UIN="+qq+"&SID=&XP=C4CA4238A0B92382\n";
	}

	//取在线好友
	public String friend(String qq) {
		return "VER=1.4&CON=1&CMD=Query_Stat2&SEQ="+seq()+"&UIN="+qq+"&SID=&XP=C4CA4238A0B92382&CM=2&UN=0\n";
	}
	
	//发送消息
	public String sendMsg(String qq,String friendqq,String msg) {
		return "VER=1.4&CON=1&CMD=CLTMSG&SEQ= "+seq()+"&UIN= "+qq+"&SID=&XP=C4CA4238A0B92382&UN= "+friendqq+"&MG="+msg+"\n";
	}
	
	/*
	 * 
	 * 后面我就不写了，参照上面的方法来写
	 * 
	6:查看好友资料: VER=1.4&CON=1&CMD=GetInfo&SEQ=标识&UIN=QQ&SID=&XP=C4CA4238A0B92382&LV=2&UN=好友

			7:隐身:               VER=1.4&CON=1&CMD=Change_Stat&SEQ=标识&UIN=QQ&SID=&XP=C4CA4238A0B92382&ST=40
			8:离开:               VER=1.4&CON=1&CMD=Change_Stat&SEQ=标识&UIN=QQ&SID=&XP=C4CA4238A0B92382&ST=30
			9:在线:               VER=1.4&CON=1&CMD=Change_Stat&SEQ=标识&UIN=QQ&SID=&XP=C4CA4238A0B92382&ST=10
			状态,  10 在线  30 离开  40隐身  20 

			10:查找好友:     VER=1.4&CON=1&CMD=AddToList&SEQ=标识&UIN=QQ&SID=&XP=C4CA4238A0B92382&UN=好友
			11:验证:             VER=1.4&CON=1&CMD=Ack_AddToList&SEQ=标识&UIN=QQ&SID=&XP=C4CA4238A0B92382&UN=好友&CD=2&RS=验证文本

			发送的数据后面都有个换行符 
			
			记得每隔一两分钟，进行一次 上线() 的操作（上面有协议，自己照着写个方法就OK了）。否则腾讯会作掉线处理。
*/
	
	
	//每次发送数据包.这个SEQ就递增
	private int SEQ = 10;
	private int seq() {
		return ++SEQ;
	}
	


	
}
